import Decimal from "decimal.js";
import * as moment from "moment";
import { getRandomChildFromArray } from "src/utils";
const crypto = require('crypto');
/**词条常量 */
export let propsKeys = {
	1: {
		key: "attack",
		value: 0,
		type: "number"
	},
	2: {
		key: "hp",
		value: 0,
		type: "number"
	},
	3: {
		key: "collisionReduce",
		value: 0,
		type: "number"
	},
	4: {
		key: "bulletReduce",
		value: 0,
		type: "number"
	},
	5: {
		key: "hurtReduce",
		value: 0,
		type: "number"
	},
	6: {
		key: "criticalTitRate",
		value: 0,
		type: "percent"
	},
	7: {
		key: "criticalTitDamage",
		value: 0,
		type: "percent"
	},
	8: {
		key: "dodge",
		value: 0,
		type: "percent"
	},
	9: {
		key: "hpResume",
		value: 0,
		type: "number"
	},
	10: {
		key: "moveSpeed",
		value: 0,
		type: "number"
	},
	11: {
		key: "maxPower",
		value: 0,
		type: "number"
	},
}
/**从词条转成标准属性对象 */
export function createNormPropsFromAffixs(data: any[]) {
	let props = {}
	for (let i = 0; i < data.length; i++) {
		const element = data[i];
		let item = propsKeys[element.affix]
		if (props[item.key]) {
			props[item.key].value = new Decimal(props[item.key].value).plus(element.value).toNumber()
		} else {
			props[item.key] = {
				value: element.value,
				type: item.type
			}
		}
	}
	return props
}
/**获取propskeys的项 */
export function getPropsConfigItem(key: string | number) {
	let res = null
	if (isNaN(Number(key))) {
		for (const propsKey in propsKeys) {
			if (Object.prototype.hasOwnProperty.call(propsKeys, propsKey)) {
				const item = propsKeys[propsKey];
				if (item.key == key) {
					res = item;
					break;
				}
			}
		}
	} else {
		res = propsKeys[key]
	}
	return res
}
/**获取数组重复项*/
export function getArrayDuplicates(arr: any[]) {
	const uniqueSet = new Set();
	const duplicates = [];
	for (let i = 0; i < arr.length; i++) {
		if (uniqueSet.has(arr[i])) {
			duplicates.push(arr[i]);
		} else {
			uniqueSet.add(arr[i]);
		}
	}
	let unique = Array.from<any>(uniqueSet)
	unique = unique.filter(item => item != undefined)
	return { duplicates, unique: unique };
}
/**从数组里随机获取一项 */
export function getRandomValue(arr) {
	const randomIndex = Math.floor(Math.random() * arr.length);
	return arr[randomIndex];
}

/**深拷贝 */
export function deepClone(obj) {
	if (typeof obj !== 'object' || obj === null) {
		return obj;
	}

	let clone = Array.isArray(obj) ? [] : {};

	for (let key in obj) {
		if (obj.hasOwnProperty(key)) {
			clone[key] = deepClone(obj[key]);
		}
	}

	return clone;
}


/**传输签名 */
export function generateSign(params, key: string) {
	// 过滤掉值为空字符串和键为"sign"的参数
	const filteredParams = Object.fromEntries(
		Object.entries(params).filter(([key, value]) => key !== 'sign' && value !== '')
	);

	// 按键值进行升序排序
	const sortedKeys = Object.keys(filteredParams).sort();

	// 组合成“key=value”的形式
	const keyValuePairs = sortedKeys.map(key => `${key}=${filteredParams[key]}`);

	// 使用"&"字符连接参数
	let concatenatedString = keyValuePairs.join('&');
	concatenatedString += "&" + key
	console.log(concatenatedString)
	// 使用MD5生成32位小写字符串
	const md5 = crypto.createHash('md5');
	const sign = md5.update(concatenatedString).digest('hex');
	return sign;
}


/**找到数组指定元素的前一个元素 */
export function findPreviousElement(arr, value) {
	let index = arr.findIndex(element => element === value);
	if (index > 0) {
		return arr[index - 1];
	} else {
		return null; // 如果找不到或者输入的值是数组的第一个元素，则返回null
	}
}

/**合并相同clientId的数组 */
export function mergeArrayMembers(arr) {
	let result = [];
	let mergedMembers = {};

	for (let i = 0; i < arr.length; i++) {
		const member = arr[i];
		if (member.type == "0" || member.type == "1" || member.type == "2" || member.type == "7") {
			if (mergedMembers.hasOwnProperty(member.clientId)) {
				mergedMembers[member.clientId].count += member.count;
			} else {
				mergedMembers[member.clientId] = { ...member };
			}
		} else {
			result.push(member);
		}
	}

	for (const clientId in mergedMembers) {
		result.push(mergedMembers[clientId]);
	}
	result = result.filter(item => item.count > 0)
	return result.sort((a, b) => Number(a.type) - Number(b.type));
}

export async function sequencePromiseAll(promises) {
	let result = []
	for (let i = 0; i < promises.length; i++) {
		let func = promises[i];
		let res = await Promise.all([func])
		result.push(...res)
	}
	return result
};

export function getCurrentDate(date?:Date){
	if(date){
		return moment(date).toDate()
	}else{
		return moment().toDate()
	}
}
